﻿@using Library
@using Library.Models;
@using Library.MVCHelpers
@model Dictionary<Menu, List<Menu>>

@if (Model.Any())
{
    bool isActived = false;
    Menu activeMenu = null;
    <ul class="nav navbar-nav">
        @foreach (var menu in Model)
        {
            var activeClass = string.Empty;
            if (!isActived && !string.IsNullOrEmpty(ViewBag.ActiveUrl))
            {
                ViewBag.ActiveUrl = ViewBag.ActiveUrl.ToLower();

                if (!string.IsNullOrEmpty(menu.Key.Url) && menu.Key.Url.ToLower().Equals(ViewBag.ActiveUrl))
                {
                    activeClass = "active";
                    isActived = true;
                    activeMenu = menu.Key;
                }
                else if (menu.Value != null)
                {
                    activeMenu = menu.Value.FirstOrDefault(sm => !string.IsNullOrEmpty(sm.Url) && sm.Url.ToLower().Equals(ViewBag.ActiveUrl));
                    if (activeMenu != null)
                    {
                        activeClass = "active";
                        isActived = true;
                    }
                }
            }

            var url = SEOHelper.GetMenuUrl(menu.Key);
            if (string.IsNullOrEmpty(url) && menu.Value != null)
            {
                <li class="dropdown @activeClass">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">@menu.Key.Text <span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">
                        @foreach (var subMenu in menu.Value)
                        {
                            if (subMenu.IsDivider.HasValue && subMenu.IsDivider.Value)
                            {
                                <li class="divider"></li>
                            }
                            else
                            {
                                <li @(activeMenu != null && activeMenu.Id == subMenu.Id ? "class=active" : "")>
                                    <a href="~/@SEOHelper.GetMenuUrl(subMenu)" title="@subMenu.Text">@subMenu.Text</a>
                                </li>
                            }
                        }
                    </ul>
                </li>
            }
            else
            {
                <li @(string.IsNullOrEmpty(activeClass) ? "" : "class=" + activeClass)>
                <a href="~/@url" title="@menu.Key.Text">@menu.Key.Text</a>
                </li>
            }
        }
    </ul>
}